{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.获取数据\n",
    "2.数据基本处理\n",
    "2.1确定特征值，目标值\n",
    "2.2缺失值处理\n",
    "2.3数据集划分\n",
    "3.特征工程（字典特征抽取）\n",
    "4.机器学习（决策树）\n",
    "5.模型评估"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "train_size/test_size分别表示训练集和测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型的准确率: 100.00%\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "# 导入相关库\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# 加载Iris数据集\n",
    "iris = load_iris()\n",
    "X = iris.data  # 特征\n",
    "y = iris.target  # 标签\n",
    "\n",
    "# 将数据集分为训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 创建决策树分类器\n",
    "clf = DecisionTreeClassifier()\n",
    "\n",
    "# 训练模型\n",
    "clf.fit(X_train, y_train)\n",
    "\n",
    "# 在测试集上进行预测\n",
    "y_pred = clf.predict(X_test)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"模型的准确率: {accuracy * 100:.2f}%\")\n",
    "print(accuracy)#输出模型分类的准确率\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入数据代码示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入相关库\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# 1. 导入数据\n",
    "data = pd.read_csv('songs_test.csv')  # 使用pandas导入数据\n",
    "\n",
    "# 2. 特征和标签的选择\n",
    "# 假设前几列是特征，最后一列是标签\n",
    "X = data.iloc[:, :-1]  # 特征：所有行，去掉最后一列\n",
    "y = data.iloc[:, -1]   # 标签：所有行，最后一列\n",
    "\n",
    "# 3. 将数据集分为训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
    "\n",
    "# 4. 创建决策树分类器\n",
    "clf = DecisionTreeClassifier()\n",
    "\n",
    "# 5. 训练模型\n",
    "clf.fit(X_train, y_train)\n",
    "\n",
    "# 6. 在测试集上进行预测\n",
    "y_pred = clf.predict(X_test)\n",
    "\n",
    "# 7. 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"模型的准确率: {accuracy * 100:.2f}%\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型的准确率:100.00%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "\n",
    "iris = load_iris()\n",
    "X = iris.data\n",
    "y = iris.target\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=42)\n",
    "\n",
    "clf = DecisionTreeClassifier()\n",
    "\n",
    "clf.fit(X_train,y_train)\n",
    "\n",
    "y_pred = clf.predict(X_test)\n",
    "\n",
    "accuracy = accuracy_score(y_test,y_pred)\n",
    "print(f\"模型的准确率:{accuracy * 100:.2f}%\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
